#!/bin/bash
echo "20230605"
source $(dirname $(readlink -f $0))/common.sh

do_prep()
{
    echo "do_prep:"
    mkdir -p ${TEMP_DIR}
}

do_pack()
{
    for (( i = 0 ; i < ${#TARGET_ARY[@]} ; i++ ))
    do
        echo "do_pack: ${TARGET_ARY[i]}.tar.gz..."
        tar -czf ${TEMP_DIR}/${TARGET_ARY[i]}.tar.gz -C ${SRC_DIR}/${TARGET_ARY[i]} .
    done
    sync
}

do_verify()
{
    for (( i = 0 ; i < ${#TARGET_ARY[@]} ; i++ ))
    do
        echo "do_verify: ${TARGET_ARY[i]}.tar.gz..."
        md5sum ${TEMP_DIR}/${TARGET_ARY[i]}.tar.gz > ${TEMP_DIR}/${TARGET_ARY[i]}.tar.gz.md5
    done
}

do_output()
{
    echo "do_output: ${DEST_FILE}.tar.gz..."
    tar -czf ${SCRIPT_DIR}/${DEST_FILE}.tar.gz -C ${TEMP_DIR}/ .
    openssl enc -e -aes-256-cbc -iter 100 -pbkdf2 -in ${SCRIPT_DIR}/${DEST_FILE}.tar.gz -out ${SCRIPT_DIR}/${DEST_FILE}.tar.gz.enc -pass file:${MISC_DIR}/${KEY_FILE}
    rm -rf ${SCRIPT_DIR}/${DEST_FILE}.tar.gz
    name_pkg="tx2_update_`cat ${MISC_DIR}/${VER_FILE}`_`date +"%Y%m%d"`"
    mv ${SCRIPT_DIR}/${DEST_FILE}.tar.gz.enc ${name_pkg}.tar.gz.enc
}

echo ===================
echo GENERATE OTA TARGET
echo ===================

do_check

do_clean

do_prep

do_pack

do_verify

do_output

do_clean

echo "done!"

